import requests
import urllib3
import json
urllib3.disable_warnings()
class Cookie:
    def __init__(self):
        self.cookie = self.login()
        self.count = self.add()
        self.orderCode = self.dingdan()
    def login(self):
        url = "https://api.zxyjsc.com/flyapi/login"
        querystring = {"version":"1.0","terminal":"1"}
        payload = {"username":"18825189765","password":"2cfdbbef4e47899c0eb9a59eb1ad1eb6","origin":"1"}
        headers = {
            'Accept-Encoding': "gzip, deflate",
            'Content-Type': "application/x-www-form-urlencoded",
            'Host': "api.zxyjsc.com",
            'Connection': "Keep-Alive",
            'User-Agent': "okhttp/3.8.0",
            'cache-control': "no-cache",
            }
        response = requests.request("POST", url, data=payload, headers=headers, params=querystring,
                                    verify=False)

        print(response.json())
        cookie_jar = response.cookies
        cookie = requests.utils.dict_from_cookiejar(cookie_jar)
        print(cookie)
        return cookie

# 加入购物车
    def add(self):
        url = "https://api.zxyjsc.com/flyapi/cart/operateCartAdd"
        querystring = {"version":"1.0","terminal":"1"}
        payload = {"skuId":"2d82eb203c1348a097aff1fdd607d2b5","quantity":"1"}
        headers = {
            'Accept-Encoding': "gzip, deflate",
            'Content-Type': "application/x-www-form-urlencoded",
            'Host': "api.zxyjsc.com",
            'Connection': "Keep-Alive",
            'User-Agent': "okhttp/3.8.0",
            'Accept': "*/*",
            'Cache-Control': "no-cache",
            'Postman-Token': "8619f0f1-5f7f-416e-b2b0-b3099ee74a03,152ed1d0-478d-4264-94c3-6f5ecebcf1fd",
            'cache-control': "no-cache"
            }
        cookie = self.cookie

        response = requests.request("POST", url, data=payload, headers=headers, params=querystring,
                                    verify=False,cookies=cookie)

        print(response.text)
        return response.json()['data']['quantity']

    def dingdan(self):
        count = self.count
        url = "https://api.zxyjsc.com/flyapi/order/add"
        querystring = {"token": "319b7d1e40614d0c81eacd99ea2bca95", "version": "1.0", "terminal": "1"}
        headers = {
            'Accept-Encoding': "gzip, deflate",
            'Content-Type': "application/json",
            'Host': "api.zxyjsc.com",
            'Connection': "Keep-Alive",
            'User-Agent': "okhttp/3.8.0",
            'Accept': "*/*",
            'cache-control': "no-cache"
        }
        payload01 = {"addressId":"867bf183c17b4ff5b8114dc146a15cd2",
                   "couponId":"",
                   "products":[{"skuId":"2d82eb203c1348a097aff1fdd607d2b5","quantity":count}],
                   "remark":"","orderFrom":1}
        payload = json.dumps(payload01)
        cookie=self.cookie
        response = requests.request("POST", url, data=payload, headers=headers, params=querystring,
                                    cookies=cookie,verify=False)

        print(response.text)
        return response.json()['data']['orderCode']

    def pay(self):
        cookie =self.cookie
        orderCode = self.orderCode
        url = "https://api.zxyjsc.com/flyapi/order/payPwd"
        querystring = {"version": "1.0", "terminal": "1"}
        headers = {
            'Accept-Encoding': "gzip, deflate",
            'Content-Type': "application/x-www-form-urlencoded",
            'Host': "api.zxyjsc.com",
            'Connection': "Keep-Alive",
            'User-Agent': "okhttp/3.8.0",
            'cache-control': "no-cache",
            'Postman-Token': "98747db6-21d7-4505-9ff0-1d73c7270975"
        }
        payload = {"orderCode":orderCode,"password":"4af1f45499806325ccd10e1cd2f85b57"}
        response = requests.request("POST", url, data=payload, headers=headers, params=querystring,verify=False,
                                    cookies=cookie)
        print(response.text)

if __name__ == '__main__':
    Cookie().pay()